package com.hdw.cms.dao.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.hdw.basic.dao.impl.BaseDaoImpl;
import com.hdw.basic.model.Pager;
import com.hdw.basic.model.SystemContext;
import com.hdw.cms.dao.AttachmentDao;
import com.hdw.cms.model.Attachment;
import com.hdw.cms.utils.UeditorUtil;

@Repository("attachmentDao")
public class AttachmentDaoImpl extends BaseDaoImpl<Attachment> implements AttachmentDao {
	public final static String UPLOAD_PATH = "/resources/upload/";
	@SuppressWarnings("unchecked")
	@Override
	public List<Attachment> listIndexNewPicByNum() {
		String hql = " from Attachment a where a.isProImg=1 and a.isIndexPic=1 order by a.proPos desc";
		return this.list(hql);
	}

	@Override
	public void updetePro(String attName, String picMessage, String proPar, int id) {
		String proAttName = UeditorUtil.getUeFileName(picMessage+proPar);
		String hql = "update Attachment a set a.attName='" + attName + "',a.picMessage='" + picMessage + "',a.proPar='"
				+ proPar + "',a.unUseFile='"+proAttName+"' where a.id='" + id + "'";
		this.UpdateByHql(hql);
	}

	@Override
	public Pager<Attachment> findProPic(Integer proTypeId) {
		String hql = "from Attachment a where a.proTypeId='" + proTypeId + "'";
		return this.find(hql);
	}

	@Override
	public Attachment findVideoImg(Integer videoId) {
		String hql = "from Attachment a where a.videoId='" + videoId + "'";
		return (Attachment) this.Queryobject(hql);
	}

	@Override
	public Pager<Attachment> findVideo(Integer proTypeId) {
		String hql = "from Attachment a where a.proTypeId='" + proTypeId + "'";
		Pager<Attachment> l = this.find(hql);
		for (int i = 0; i < l.getDatas().size(); i++) {
			l.getDatas().get(i).setVideoImg(this.findVideoImg(l.getDatas().get(i).getId()));
		}
		return l;
	}

	@Override
	public void updateIndexImg(Integer id, Integer isIndex) {
		String hql = "update Attachment a set a.isIndexPic='" + isIndex + "' where a.id='" + id + "'";
		this.UpdateByHql(hql);
	}

	@Override
	public void updatePic(Integer proTypeId, String attName, String picMessage, String proPar, int id) {
		String proAttName = UeditorUtil.getUeFileName(picMessage+proPar);
		String hql = "update Attachment a set a.proTypeId='" + proTypeId + "',a.attName='" + attName
				+ "',a.picMessage='" + picMessage + "',a.proPar='" + proPar + "',a.unUseFile='"+proAttName+"' where a.id='" + id + "'";
		this.UpdateByHql(hql);
	}

	@Override
	public Attachment loadVideo(int id) {
		Attachment video = this.load(id);
		Attachment videoImg = this.findVideoImg(id);
		video.setVideoImg(videoImg);
		return video;
	}

	@Override
	public void updateVideo(Integer proTypeId, String attName, String picMessage, int id) {
		String videoAttName = UeditorUtil.getUeFileName(picMessage);
		String hql = "update Attachment a set a.proTypeId='" + proTypeId + "',a.attName='" + attName
				+ "',a.picMessage='" + picMessage + "',a.unUseFile='"+videoAttName+"' where a.id='" + id + "'";
		this.UpdateByHql(hql);
	}

	@Override
	public Pager<Attachment> listSearchPro(String attName) {
		String hql = "from Attachment a where a.attName like '%" + attName + "%' and a.isProImg=1";
		return this.find(hql);
	}

	@Override
	public Attachment add(Attachment a) {
		if (a.getisProImg() == 1) {
			String hql = "update Attachment a set a.proPos =a.proPos+1 where a.proPos >=? and a.proTypeId=?";
			this.getSession().createQuery(hql).setParameter(0, 1).setParameter(1, a.getProTypeId()).executeUpdate();
			a.setProPos(1);
		} else if (a.getIsVideo() == 1) {
			String hql = "update Attachment a set a.vdoPos =a.vdoPos+1 where a.vdoPos >=? and a.proTypeId=?";
			this.getSession().createQuery(hql).setParameter(0, 1).setParameter(1, a.getProTypeId()).executeUpdate();
			a.setVdoPos(1);
		}
		super.add(a);
		return a;
	}

	public static void deleteAttachFiles(Attachment a) {
		String realPath = SystemContext.getRealPath();
		String thumbPath = realPath + "/resources/upload/thumbnail/";
		String NewthumbPath = realPath + "/resources/upload/Newthumbnail/";
		// new File(realPath+a.getNewName()).delete();
		new File(realPath + UPLOAD_PATH + a.getNewName()).delete();
		if (a.getisProImg()== 1 || a.getIsVideoImg()==1) {
			new File(thumbPath + a.getNewName()).delete();
			new File(NewthumbPath + a.getNewName()).delete();
		}
	}
	@Override
	public void delete(int id) {
		Attachment a = this.load(id);
		if (a.getisProImg() == 1) {
			String hql = "update Attachment a set a.proPos =a.proPos-1 where a.proPos >=? and a.proTypeId=?";
			this.getSession().createQuery(hql).setParameter(0, a.getProPos()).setParameter(1, a.getProTypeId())
					.executeUpdate();
		} else if (a.getIsVideo() == 1) {
			String hql = "update Attachment a set a.vdoPos =a.vdoPos-1 where a.vdoPos >=? and a.proTypeId=?";
			this.getSession().createQuery(hql).setParameter(0, a.getVdoPos()).setParameter(1, a.getProTypeId())
					.executeUpdate();
			Attachment videoImg = this.findVideoImg(id);
			if (videoImg != null) {
				deleteAttachFiles(videoImg);
				super.delete(videoImg.getId());
			}
		}
		deleteAttachFiles(a);
		super.delete(id);
	}

	@Override
	public Map<String, Integer> getMinAdnMaxProPos() {
		String hql = "select max(proPos),min(proPos) from Attachment";
		Object[] obj = (Object[]) this.getSession().createQuery(hql).uniqueResult();
		Map<String, Integer> mm = new HashMap<String, Integer>();
		mm.put("max", (Integer) obj[0]);
		mm.put("min", (Integer) obj[1]);
		return mm;
	}

	@Override
	public Map<String, Integer> getMinAdnMaxVdoPos() {
		String hql = "select max(vdoPos),min(vdoPos) from Attachment";
		Object[] obj = (Object[]) this.getSession().createQuery(hql).uniqueResult();
		Map<String, Integer> mm = new HashMap<String, Integer>();
		mm.put("max", (Integer) obj[0]);
		mm.put("min", (Integer) obj[1]);
		return mm;
	}

	@Override
	public void updateProPos(int id, int oldPos, int newPos) {
		Attachment a = this.load(id);
		if (oldPos == newPos) {
			return;
		}
		String hql = "";
		if (oldPos < newPos) {
			hql = "update Attachment set proPos=proPos-1 where proPos>? and proPos<=?";
		} else {
			hql = "update Attachment set proPos=proPos+1 where proPos<? and proPos>=?";
		}
		this.getSession().createQuery(hql).setParameter(0, oldPos).setParameter(1, newPos).executeUpdate();
		a.setProPos(newPos);
		String picAttname = UeditorUtil.getUeFileName(a.getPicMessage()+a.getProPar());
		if (picAttname != null) {
			a.setUnUseFile(picAttname);
		}
		this.update(a);
	}

	@Override
	public void updateVdoPos(int id, int oldPos, int newPos) {
		Attachment a = this.load(id);
		if (oldPos == newPos) {
			return;
		}
		String hql = "";
		if (oldPos < newPos) {
			hql = "update Attachment set vdoPos=vdoPos-1 where vdoPos>? and vdoPos<=?";
		} else {
			hql = "update Attachment set vdoPos=vdoPos+1 where vdoPos<? and vdoPos>=?";
		}
		this.getSession().createQuery(hql).setParameter(0, oldPos).setParameter(1, newPos).executeUpdate();
		a.setVdoPos(newPos);
		String picAttname = UeditorUtil.getUeFileName(a.getPicMessage()+a.getProPar());
		if (picAttname != null) {
			a.setUnUseFile(picAttname);
		}
		this.update(a);
	}

	@Override
	public List<Attachment> findVideoBytype(Integer videoTypeId) {
		String hql = "from Attachment a where a.proTypeId='" + videoTypeId + "'";
		return this.list(hql);
	}

	@Override
	public List<Attachment> listProAndVideo() {
		String hql="from Attachment a where a.isProImg='1' or a.isVideo='1'";
		return this.list(hql);
	}

	@Override
	public void updatePath(Attachment t) {
		String proParp=t.getProPar().replaceAll("http://ys.xujin.org", "http://localhost:8080/cms-qzsk");
		String picMessage=t.getPicMessage().replaceAll("http://ys.xujin.org", "http://localhost:8080/cms-qzsk");
		String hql="update Attachment set proPar='"+proParp+"',picMessage='"+picMessage+"' where id ='"+t.getId()+"'";
		this.getSession().createQuery(hql).executeUpdate();
	}

	@Override
	public List<Attachment> listAttByProType(int typeId) {
		String hql="from Attachment where proTypeId='"+typeId+"'";
		return this.list(hql);
	}
}
